using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using System.Threading;
using System.Globalization;


using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;


namespace listsourcetokens
{
    class listsourcetokens
    {
        static string gethex(string pStr)
        {
            string ret="";
            System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
            //System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] bs = encoding.GetBytes(pStr);
            foreach (byte b in bs)
            {
                ret = ret + b.ToString("X2")+" ";
            }
            return ret;
        }

        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("{0} scriptfile", Environment.GetCommandLineArgs()[0]);
                return;
            }

            string inputfile = args[0];


            Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");

            // create a sql parser instance supports ms sql server sql dialect
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVOracle);

            //Console.WriteLine("Input sql in hex");
            //Console.WriteLine(gethex(lcsqltext));
            //sqlparser.SqlText.Text = lcsqltext;
            sqlparser.Sqlfilename = inputfile;
            //Console.WriteLine("\nsqlparser sqltext in hex");
            //Console.WriteLine(gethex(sqlparser.SqlText.Text));

            //Console.WriteLine("");
            // sqlparser.Sqlfilename = "a.sql";
            sqlparser.SQLTextToTokenList();
            //sqlparser.Parse();

            if (sqlparser.SourceTokenList.Count() > 0)
            {
                foreach (TSourceToken st in sqlparser.SourceTokenList)
                {
                    //Console.WriteLine("Text: {0} \nType: {1} \nhex: {2}\n", st.AsText, st.TokenType.ToString(), gethex(st.AsText));
                    Console.WriteLine("{0},{1},{2},{3},{4},{5}", st.AsText,st.XPosition, st.YPosition, st.AsText.Length, st.Offset, st.TokenCode);
                }
                Console.Write(sqlparser.SqlText.Text);
            }
            else
            {
                Console.WriteLine("no source token was found");
            }
     
        }
    }
}
